package com.stress.testing.plugin.es.strategy;

import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.script.mustache.MultiSearchTemplateRequest;
import org.elasticsearch.script.mustache.SearchTemplateRequest;

import java.util.Iterator;
import java.util.List;

public class MultiSearchTemplateRequestIndexRenameStrategy extends AbstractRequestIndexRenameStrategy {
    @Override
    public void renameIndex(Object arg) {
        MultiSearchTemplateRequest request = (MultiSearchTemplateRequest) arg;
        List<SearchTemplateRequest> requests = request.requests();
        if (requests == null || requests.size() == 0) {
            return;
        }

        Iterator<SearchTemplateRequest> iterator = requests.iterator();
        while (iterator.hasNext()) {
            SearchTemplateRequest searchTemplateRequest = iterator.next();
            SearchRequest searchRequest = searchTemplateRequest.getRequest();
            searchRequest.indices(internalRenameIndex(searchRequest.indices()));
        }
    }
}
